package com.cty.listener; /**
 * @Auther: cty
 * @Date: 2020/2/23 21:48
 * @Description:
 * @version: 1.0
 */

import com.cty.pojo.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

@WebListener()
public class OnLineListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public OnLineListener() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
        ServletContext sc = sce.getServletContext();
        sc.setAttribute("count", 0);
        System.out.println("在线人数："+0);
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
        HttpSession hs = se.getSession();
        User user = (User)hs.getAttribute("user");
        if(null==user){
            System.out.println("创建了一个用户session：");
        }else{
            System.out.println("创建了一个用户session："+user.getUname());
        }

        ServletContext sc = se.getSession().getServletContext();
        int count = (int)sc.getAttribute("count");
        count++;
        System.out.println("在线人数："+count);
        sc.setAttribute("count", count);
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
        HttpSession hs = se.getSession();
        User user = (User)hs.getAttribute("user");
        if(null==user){
            System.out.println("销毁了一个用户session：");
        }else{
            System.out.println("销毁了一个用户session："+user.getUname());
        }

        ServletContext sc = se.getSession().getServletContext();
        int count = (int)sc.getAttribute("count");
        count--;
        System.out.println("在线人数："+count);
        sc.setAttribute("count", count);
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attribute
         is replaced in a session.
      */
    }
}
